package com.soso.controller;

import java.io.UnsupportedEncodingException;

import java.util.HashMap;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.soso.dao.UsersDao;
import com.soso.entity.Users;

import com.soso.util.TokenUtil;

@RestController
@CrossOrigin
public class LoginController {
	@Autowired
	private UsersDao usersdao;
	
	
	
	@RequestMapping("/test")
	public String test(){

		return "ok";
	}
	
	@GetMapping("/login")
	public Map<String, Object> login(String LoginName, String Password) {
		
		String token = null;
		
		Users admin=usersdao.getlogin(LoginName, Password);
		
		Map<String, Object> map = new HashMap<>();
		if(admin!=null) {
			
			if (admin.getLoginName().equals(LoginName) && admin.getPassword().equals(Password)) {
				try {
					token = TokenUtil.sign(admin, 1 * 60 * 60 * 1000); // 1*60*60 * 1000 = 1小时有效期
					System.out.println("生成token大小=>" + token.length());
					System.out.println(token);
				} catch (UnsupportedEncodingException | JsonProcessingException e) {
					e.printStackTrace();
				}
				
				Integer o=usersdao.orisLockoutlogin(admin.getId());
				map.put("token", token);
				map.put("LoginName", admin.getLoginName());
				map.put("id", admin.getId());
			
				map.put("code", "1");
			}
		}else {
			
				map.put("code", "0");
			
		}

		return map;
	}
}